package Qverst::CSS::Optimize::Margin;
use Moose::Role;

use Carp;
use CSS::Builder::Element::Simple;

requires 'simple';

sub optimize_margin {
      my ($self , $property_margin , $mode) = @_;
      
      my @can = qw(top right bottom left);
      my $margin = $property_margin->margin;
            
      if ($margin) { # we have some margin and we need to analyze it
            
            if ($property_margin->margin->inherit) {
                  
                  # if it's inherited value ?
                  if ($property_margin->margin->important) {
                        
                        # search for top-right-bottom-left: inherit !important
                        foreach my $prop (@can) {
                              my $value = $property_margin->$prop;
                              if ($value) {
                                    if ($property_margin->$prop->inherit and $property_margin->$prop->important) {
                                          
                                          $property_margin->$prop('');
                                    }
                              }
                        }
                        
                  } else {
                          
                        # search for top-right-bottom-left: inherit
                        foreach my $prop (@can) {
                              my $value = $property_margin->$prop;
                              if ($value) {
                                    if ($property_margin->$prop->inherit) {
                                          $property_margin->$prop('');
                                    }
                              }
                        }
                        
                  }
                     
            } else {
                  #map with values
                  my %map = ();
                  
                  my $margin_val = $margin->value;
                  # make sure there is only 1 space between values
                  $margin_val =~ s/\s+/ /g;
                  
                  # split it into top-right-bottom-left
                  my @trbl = split / / , $margin_val;
                  
                  # we need exectly 4 values in trbl array
                  unless ($#trbl eq '3') {
                        my @temp;
                        if ($#trbl eq '0') {
                              push @temp , ($trbl[0] , $trbl[0] , $trbl[0] , $trbl[0]);
                        } elsif ($#trbl eq '1') {
                              push @temp , ($trbl[0] , $trbl[1] , $trbl[0] , $trbl[1]);
                        } elsif ($#trbl eq '2') {
                              push @temp , ($trbl[0] , $trbl[1] , $trbl[2] , $trbl[1]);
                        }
                        
                        @trbl = @temp;
                  }
                  
                  for (my $i = 0; $i <= $#can; $i++) {
                        $map{$can[$i]} = $trbl[$i];
                  }
                  
                  #use Data::Dumper;
                  #print Dumper \%map;
                  
                  if ($property_margin->margin->important) {
                        # then we search only for important values
                        foreach my $prop (@can) {
                              my $value = $property_margin->$prop;
                              if ($value) {
                                    if ($property_margin->$prop->important and
                                        $property_margin->$prop->value eq $map{$prop}
                                    ) {
                                          $property_margin->$prop('');
                                    }
                              }
                        }
                  } else {
                        foreach my $prop (@can) {
                              my $value = $property_margin->$prop;
                              if ($value) {
                                    if ($property_margin->$prop->value eq $map{$prop}) {
                                          $property_margin->$prop('');
                                    }
                              }
                        }
                  }
            }
      } else { # maybe we should add margin-value ?
            # 
            my @val = ();
            my $margin = CSS::Builder::Element::Simple->new();
            
            foreach my $prop (@can) {
                  my $value = $property_margin->$prop;
                  if ($value) {
                        push @val , $value->value;
                        if ($value->important) {
                              $margin->important(1);
                              last;
                        }
                  } else {
                        push @val , 0;
                  }
            }
            
            # check if it's inherit
            my $inh = 0;
            foreach my $prop (@can) {
                  my $value = $property_margin->$prop;
                  if ($value) {
                        if ($value->inherit) {
                              $inh++;
                        }
                  }
            }
            
            if ($inh eq 4) {
                  $margin->inherit(1);
            } elsif ($inh eq 0) {
                  if($val[0] eq $val[1] and
                     $val[0] eq $val[2] and
                     $val[0] eq $val[3]
                  ) {
                        $margin->value($val[0]);
                  } elsif ($val[1] eq $val[3] and
                           $val[0] eq $val[2]
                  ) {
                        $margin->value($val[0] . " " . $val[1]);
                  } elsif ($val[1] eq $val[3]) {
                        $margin->value($val[0] . " " . $val[1] . " " . $val[2]);
                  } else {
                        $margin->value(join " " , @val);
                  }
            }
            
            if ($margin->inherit or $margin->value) {
                  $property_margin->margin($margin);
                  foreach my $prop (@can) {
                        $property_margin->$prop('');
                  }
            }
      }
}

1;
